package com.yiren.dbaa.module.practice_set.line.presenter;

import android.app.Activity;
import android.graphics.drawable.Drawable;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.dbaa.R;
import com.yiren.dbaa.module.practice_set.line.contract.BaiduMapContract;
import com.yiren.dbaa.module.practice_set.line.model.BaiduMapModel;

import java.util.List;

/**
 * Describe
 *
 * @author WRX
 * @date 2022/11/21
 */
public class BaiduMapPresenter implements BaiduMapContract.Presenter {

    private Activity mActivity;
    private BaiduMapContract.Model baiduMapModel;

    private BaiduMap mBaiduMap;
    /*** 是否刷新地图 */
    public boolean isRefreshOption = true;
    /*** 第一次定位成功 */
    private boolean isFirstSuccess = true;
    /*** 根据线路点位绘制百度地图标识物 */
    private boolean isExecuteThread = false;

    public BaiduMapPresenter(Activity mActivity) {
        this.mActivity = mActivity;
        baiduMapModel = new BaiduMapModel();
    }

    @Override
    public BaiduMap LoadBaiduMap(MapView mapView, boolean isPositioning) {
        if (mBaiduMap != null) mBaiduMap.clear();
        mBaiduMap = baiduMapModel.getBaiduMap(mapView);
        if (isPositioning) {
            BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.car);
            MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
            mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(mCurrentMode, false, mCurrentMarker));
        }
        GpsInfoBean gpsInfo = GpsInfoBean.getInstance();
        if (null != gpsInfo) {
            MyLocationData locData = new MyLocationData.Builder().accuracy(20).direction(gpsInfo.getDirection()).latitude(gpsInfo.getLatitude()).longitude(gpsInfo.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            LatLng ll = new LatLng(gpsInfo.getLatitude(), gpsInfo.getLongitude());
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(ll).zoom(19);
            if (null != mBaiduMap) {
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        }
        return mBaiduMap;
    }

    @Override
    public void baiduMapGpsListener() {
        new Thread(() -> {
            while (isRefreshOption) {
                GpsInfoBean gps = GpsInfoBean.getInstance();
                // 判断GPS是否定位成功，否则不能录制
                if (gps == null || !gps.isLocationSuccess(mActivity)) {
                    Drawable drawable = mActivity.getResources().getDrawable(R.drawable.gps_no_normal);
                    // 这一步必须要做,否则不会显示.
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                } else {
                    baiduMapModel.updateLocationInfo(mBaiduMap, gps.getBearing(), gps.getLatitude(), gps.getLongitude());
                    if (isFirstSuccess) {
                        Drawable drawable = mActivity.getResources().getDrawable(R.drawable.gps_normal);
                        // / 这一步必须要做,否则不会显示.
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        isFirstSuccess = false;
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void drawBaiduMarker(List<LinePointBean> pointList, BaiduMap mBaiduMap, int currentPointIndex) {
        if (null != mBaiduMap) {
            mBaiduMap.clear();
        }
        if (!isExecuteThread) {
            isExecuteThread = true;
            try {
                baiduMapModel.drawBaiduMarker(mActivity, pointList, mBaiduMap, currentPointIndex);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                isExecuteThread = false;
            }
        }
    }

    @Override
    public void stopRefreshOption(boolean isRefreshOption) {
        this.isRefreshOption = isRefreshOption;
        if (null != mBaiduMap) {
            mBaiduMap.clear();
            mBaiduMap = null;
        }

    }
}
